// pages/cat_mse_look/cat_mse_look.js
const app = getApp()
const date = new Date() 
var that
Page({
  data: {
    actionSheetHidden_catpic: true, //隐藏修改猫咪头像选项
    actionSheetHidden_pic: true,   //下方是否隐藏删除猫咪的选项
    cat_picurl: "http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg", //个人头像路劲
    cat_id:" ",
    nickname:"",
    variety:"",
    sex:"",
    age:"",
    ster_status:"",
    remark:"猫咪简介"
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this;
    // console.log(options.cat_id);
    that.setData({ cat_id: options.cat_id });
    that.get_cat_msg(that.data.cat_id);         
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  onShow: function () {
    that.get_cat_msg(that.data.cat_id);         
  }, 
 
  ActionSheet_select: function ()//下方选项取消后即隐藏
  {
    this.setData({ actionSheetHidden_pic: true,actionSheetHidden_catpic: true})
  },
  itemclick_catpic:function(e){
    this.setData({ actionSheetHidden_catpic: true })//选好后即隐藏
    if (e.currentTarget.id == 1) {//相机
      console.log("相机");
      wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['camera'],  // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          var tempFilePaths = res.tempFilePaths
          wx.uploadFile({
            url: app.rootDocment + 'upload.html',
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {
              'sessionid': wx.getStorageSync('3rd_session'),
              'openid': app.data.openid,
              'type': "avter",
              "cat_id":that.data.cat_id
            },
            success: function (res) {
              // console.log(res);//设置头像
              var j = JSON.parse(res.data);
              that.setData({ cat_picurl:j.mess});
            }
          })
        }
      })
    }
    if (e.currentTarget.id == 2) { //相册
      console.log("相机22");
      wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths
          wx.uploadFile({
            url: app.rootDocment + 'upload.html',
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {
              'sessionid': wx.getStorageSync('3rd_session'),
              'openid': app.data.openid,
              'type': "avter",
              "cat_id": that.data.cat_id
            },
            success: function (res) {
              // console.log(res);//设置头像
              var j = JSON.parse(res.data);
              that.setData({ cat_picurl: j.mess });
            }
          })
        }
      })
    }
   
  },
  itemclick: function (e) {//点击选项时触发
    var that = this;
    if (e.currentTarget.id == 2) {  //确定
      app.req("delete_cat.html", {cat_id: that.data.cat_id}, function (res) {
        // console.log(res);
        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 2000
        })
        wx.navigateBack({//返回上一级
          delta: 1
        })
      }, function (res) {
        console.log(res);
      });         
    }
    this.setData({ actionSheetHidden_pic: true })
  },
  dlt:function(){  //点击删除弹出确认删除选项
    this.setData({ actionSheetHidden_pic: false })
  },


  photo_change: function () {//点击猫咪头像，改变猫咪头像
    this.setData({ actionSheetHidden_catpic: false })
  },
  cat_sex:function(){//跳转到修改猫咪性别界面
    wx.navigateTo({
      url: './cat_sex?sex=' + that.data.sex + '&id=' + that.data.cat_id,
    })
  },
  nickname_change: function () {//跳转到修改猫咪昵称
    wx.navigateTo({
      url: './cat_nickname?nickname=' + that.data.nickname + '&id=' + that.data.cat_id,
    })
  },
  age_change: function () {//跳转到修改猫咪 年龄
    wx.navigateTo({
      url: './cat_birth?id=' + that.data.cat_id,
    })
  },
  ster_change: function () {//跳转到修改猫咪绝育
    wx.navigateTo({
      url: './cat_ster?ster=' + that.data.ster_status + '&id=' + that.data.cat_id,
    })
  },
  remark_change: function (e) { //跳转到修改猫咪简介
    
    wx.navigateTo({
      url: './cat_remark?remark=' + that.data.remark + '&id=' + that.data.cat_id,
    })
  },
  cat_age: function (y, m, d) {
    var n_year = date.getFullYear()
    var n_month = date.getMonth() + 1
    var n_day = date.getDate()
    if ((n_year - y) * 12*365 + (n_month - m) * 30 + (n_day - d) < 0) {
      //猫还没生下来呢
      console.log("猫还没生下来呢");
    } else {
      var mon = (n_year - y) * 12 + (n_month - m);
      that.setData({ age: ((mon / 12) - 0.5).toFixed(0) + "岁" + mon % 12 + "个月" });
      if (((mon / 12) - 0.5).toFixed(0) <= 0) {
        that.setData({ age: mon % 12 + "个月" });
      }
      // console.log(that.data.age);
    }
  },
  get_cat_msg:function(cat_id){
    app.req("get_cat_info.html", { cat_id: cat_id }, function (res) {
      // console.log(res);
      var m = res.mess;
      if (m.sex == 1) {
        that.setData({ sex: "GG" });
      } else { that.setData({ sex: "MM" }); }
      if (m.ster_status == 1) {
        that.setData({ ster_status: "是" });
      } else { that.setData({ ster_status: "否" }); }
      var b = m.birthday.split('-');
      that.cat_age(b[0], b[1], b[2]);
      that.setData({ cat_id: m.id, cat_picurl: m.photo, nickname: m.nickname, variety: m.variety, remark: m.remark, sick_history: m.sick_history });
    }, function (res) {
      console.log(res);
    });
  }
})